home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SNNSV32.ZIP / SNNSv3.2 / tools / sources / convert2snns.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-25  |  11.6 KB  |  416 lines

  1. /*****************************************************************************
  2.   FILE           : convert2snns.c
  3.   SHORTNAME      : convert2snns.c
  4.   SNNS VERSION   : 3.2
  5.  
  6.   PURPOSE        : Network Generator for n-Component Kohonen Networks
  7.   NOTES          :
  8.  
  9.   AUTHOR         : Marc Seemann & Marcus Ritt 
  10.   DATE           : 24.2.92
  11.   VERSION        : 2.0
  12.   UPDATE         : 13.7.93
  13.  
  14.   CHANGED BY     : 
  15.   IDENTIFICATION : @(#)convert2snns.c    1.4 3/24/94
  16.   SCCS VERSION   : 1.4
  17.   LAST CHANGE    : 3/24/94
  18.  
  19.              Copyright (c) 1990-1994  SNNS Group, IPVR, Univ. Stuttgart, FRG
  20.  
  21. ******************************************************************************/
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26.  
  27. /*  SNNS-Kernel constants and data type definitions  */
  28. #include "glob_typ.h"
  29. /*  SNNS-Kernel User-Interface Function Prototypes   */
  30. #include "kr_ui.h"
  31.  
  32. /*########################################################################
  33.     PROTOTYPE - Section
  34. #########################################################################*/
  35.  
  36. void    errChk (int);
  37. void    quit   (void);
  38. void    control(FILE *);        
  39. void    create_network(char *);
  40. void    create_patterns(char *,int);
  41. int     main (int,char **);
  42.  
  43. /*########################################################################
  44.     global definitions  
  45. #########################################################################*/
  46.  
  47. /* Learning function name */
  48. #define KOHONEN_LEARN_FUNC_NAME     "Kohonen"
  49.  
  50. /* Update function name */
  51. #define KOHONEN_UPDATE_FUNC_NAME    "Kohonen_Order"
  52.  
  53. /* Init functionname */
  54. #define KOHONEN_INIT_FUNC_NAME      "KOHONEN_Weights"
  55.  
  56. char   *control_file,pattern_file[80],weight_file[80],
  57.        string[80],name[50];
  58. int    no_of_exemplars,X,Y,
  59.        ret, i, j, unit_no,
  60.        IUnits, OUnits, HUnits;
  61.  
  62. float  initialize_params[5];
  63. struct PosType    unit_pos;
  64.  
  65. char keywords[][20] = { "xsize", "ysize", "components", "patterns", "weightfile", "patternfile", "decrease", "height", "radius" };
  66. #define NONE -1
  67. #define XSIZE 0
  68. #define YSIZE 1
  69. #define COMPONENTS 2
  70. #define PATTERNS 3
  71. #define WEIGHTFILE 4
  72. #define PATTERNFILE 5
  73. #define DECREASE 6
  74. #define HEIGHT 7
  75. #define RADIUS 8
  76. #define NKEYW 8           /* Number of Keywords */
  77.   
  78. /*########################################################################
  79.     FUNCTION - Section
  80. #########################################################################*/
  81.  
  82. /*****************************************************************************
  83.   FUNCTION : errChk
  84.  
  85.   PURPOSE  : Check whether an error occured during a process
  86.   NOTES    :
  87.  
  88.   UPDATE   : june 15 1993
  89. ******************************************************************************/
  90.  
  91. void    errChk(int errNo )
  92. {
  93.   if (errNo != 0)
  94.     {
  95.     printf( "%s\n", krui_error( errNo ));
  96.     exit( 1 );
  97.     }
  98. } /* end of errChk */
  99.  
  100. /*****************************************************************************
  101.   FUNCTION : control
  102.  
  103.   PURPOSE  : get information from the control file
  104.   NOTES    : 
  105.  
  106.   UPDATE   : july 13 1993
  107. ******************************************************************************/
  108.  
  109.  
  110. void control(FILE *fp)
  111. {
  112.  
  113. int   components=0, xsize=0, ysize=0, adapt_radius=0;
  114. int   no_of_pus, no_of_learnsteps, maxtrain;
  115. float adapt_height=0, mul=0;
  116. float val;
  117.  
  118. char key[255];
  119. short point;
  120.  
  121.   while (!feof(fp)) {
  122.     fscanf(fp,"%s",key);
  123.     for(point=0;key[point];point++) key[point]=tolower(key[point]);
  124.     for(point=NKEYW;point>=0;point--) if (!strcmp(keywords[point],key)) break;
  125.  
  126.     switch(point) {
  127.       case NONE       : break;
  128.       case XSIZE      : fscanf(fp,"%d",&xsize); break;
  129.       case YSIZE      : fscanf(fp,"%d",&ysize); break;
  130.       case COMPONENTS : fscanf(fp,"%d",&components); break; 
  131.       case PATTERNS   : fscanf(fp,"%d",&no_of_exemplars); break;
  132.       case WEIGHTFILE : fscanf(fp,"%s",weight_file); break;
  133.       case PATTERNFILE: fscanf(fp,"%s",pattern_file); break;
  134.       case HEIGHT     :
  135.       case DECREASE   :
  136.       case RADIUS     : break;
  137.     }
  138.     fscanf(fp,"%[^\n]s",key);       /* ignores comments at end of line */
  139.   }
  140.  
  141.   printf("\nThe File %s contains the following Datas:\n",control_file);
  142.   printf("\nPattern File: %s ",pattern_file);
  143.   printf("\nWeight File: %s ",weight_file);
  144.   printf("\nX-Size: %d",xsize);
  145.   printf("\nY-Size: %d",ysize);
  146.   printf("\nComponents: %d",components);
  147.   printf("\nNo. of Patterns: %d",no_of_exemplars);
  148.   printf("\nAdaptation Height: %f",adapt_height);
  149.   printf("\nAdaptation Radius: %d",adapt_radius);
  150.   printf("\nDecrease Factor: %f",mul);
  151.  
  152.   no_of_pus=xsize*ysize;
  153.   printf("\nNetwork consists of %d PUs",no_of_pus);
  154.   fclose(fp);
  155.  
  156.   IUnits = components;
  157.   OUnits = 0;
  158.   X = xsize;
  159.   Y = ysize;
  160.  
  161.   printf( "\nNo. of Components (No. of Input Units): " );
  162.   printf( "%i", IUnits );
  163.   printf( "\nNo. of Output Units(0 for don`t care): " );
  164.   printf( "%i",OUnits );
  165.   printf( "\nDimension of the Feature Map [X Y](No. of Hidden Units=X*Y): " );
  166.   printf( "%i %i", X, Y );
  167.   HUnits = X*Y;
  168.  
  169. } /* end of control */
  170.  
  171. /*****************************************************************************
  172.   FUNCTION : create_network 
  173.  
  174.   PURPOSE  : create the Kohonen Network with datas from control file 
  175.   NOTES    : If no weight file is specified the weights will be set to zero
  176.  
  177.   UPDATE   : june 15 1993
  178. ******************************************************************************/
  179.  
  180. void create_network(char *weight_file)
  181.  
  182. {
  183.  FILE *fp;
  184.  float val;
  185.  
  186. /*  Allocate units (the user may or may not use this function, 
  187.       there is no need to do this)  */
  188.  
  189.        ret = krui_allocateUnits( OUnits + HUnits + IUnits );
  190.        errChk( ret );
  191.        printf( "\n\nCreate Units now\n" );
  192.  
  193. /*  Create standard (input) Units  */
  194.  
  195.        unit_pos.x = 1;
  196.        for (i = 1; i <= IUnits; i++)
  197.      {
  198.        unit_no = krui_createDefaultUnit();
  199.        if (unit_no < 0)  errChk( unit_no );
  200.        ret = krui_setUnitTType( unit_no, INPUT );
  201.        errChk( ret );
  202.  
  203.        unit_pos.y = (IUnits<Y)?i+(Y-IUnits)/2:i;
  204.        krui_setUnitPosition( unit_no, &unit_pos );
  205.      }
  206.  
  207. /*  Create standard (hidden) Units  */
  208.   
  209.        for (i = 1; i <= Y; i++)
  210.      for (j = 1; j <= X; j++)
  211.        {
  212.          unit_pos.x = 4+j;
  213.          unit_no = krui_createDefaultUnit();
  214.          if (unit_no < 0)  errChk( unit_no );
  215.          ret = krui_setUnitTType( unit_no, HIDDEN );
  216.          errChk( ret );
  217.  
  218.          unit_pos.y = i;
  219.          krui_setUnitPosition( unit_no, &unit_pos );
  220.        }
  221.  
  222. /*  Create standard (output) Units  */
  223.  
  224.        unit_pos.x = 4+X+3;
  225.        if (OUnits) for (i = 1; i <= OUnits; i++)
  226.      {
  227.        unit_no = krui_createDefaultUnit();
  228.        if (unit_no < 0)  errChk( unit_no );
  229.        ret = krui_setUnitTType( unit_no, OUTPUT );
  230.        errChk( ret );
  231.  
  232.        unit_pos.y = (OUnits<Y)?i+(Y-OUnits)/2:i;
  233.        krui_setUnitPosition( unit_no, &unit_pos );
  234.      }
  235.  
  236. /* Make Connections now */
  237. /* Make connections between hidden units and output units first !  */
  238.  
  239.        for (i = IUnits + HUnits + 1; i <= IUnits + HUnits + OUnits; i++)
  240.      {  /*  Make output unit to current unit  */
  241.        ret = krui_setCurrentUnit( i );
  242.        errChk( ret );
  243.  
  244.        for (j = IUnits + 1; j <= IUnits + HUnits; j++)
  245.          {  /*  connect current (output) unit with hidden unit. 
  246.                     REMEMBER: The hidden unit #j is the predecessor of
  247.                         the (output) unit #i (it is a backward connection) */
  248.            ret = krui_createLink( j, 0 );
  249.            errChk( ret );
  250.          }
  251.      }
  252.  
  253. /* Make connections between input units and hidden units
  254.    and set link weight with datas from output_file   */
  255.  
  256.        printf("\nSet link weights now\n");
  257.        if((fp=fopen(weight_file,"r"))!=NULL)
  258.         for (i = IUnits + 1; i <= IUnits + HUnits; i++)
  259.      {  /*  Make hidden unit to current unit  */
  260.        ret = krui_setCurrentUnit( i );
  261.        errChk( ret );
  262.  
  263.        for (j = 1; j <= IUnits; j++)
  264.          { /*  (backward) connect current (hidden) unit with input unit  */
  265.            fscanf(fp,"%s",string);
  266.                val = atof(string);
  267.                ret = krui_createLink( j,val);
  268.            errChk( ret );
  269.          }
  270.      }
  271.         else{ /* set all link weights to zero */
  272.       for (i = IUnits + 1; i <= IUnits + HUnits; i++)
  273.         {  /*  Make hidden unit to current unit  */
  274.           ret = krui_setCurrentUnit( i );
  275.           errChk( ret );
  276.           
  277.           for (j = 1; j <= IUnits; j++)
  278.         {/* (backward) connect current (hidden) unit with input unit */
  279.           ret = krui_createLink( j,0);
  280.           errChk( ret );
  281.         }
  282.         } 
  283.       
  284.       printf("\nWeight file %s could not be opened!\n",weight_file);
  285.       printf("All weights have been set to zero!\n");
  286.     }
  287.        fclose(fp);
  288.  
  289.  
  290.         /*  set the update function  */
  291.        ret = krui_setUpdateFunc (KOHONEN_UPDATE_FUNC_NAME);
  292.        errChk( ret );
  293.         /* set the learning function */
  294.        ret = krui_setLearnFunc (KOHONEN_LEARN_FUNC_NAME);
  295.        errChk( ret );
  296.         /* set the init function */
  297.        ret = krui_setInitialisationFunc (KOHONEN_INIT_FUNC_NAME);
  298.        errChk( ret );
  299.  
  300.  
  301.        printf("\nEnter Filename of the Network to save: ");
  302.        scanf("%s", name);
  303.        strcat(name,".net");
  304.        printf("Save Network\n");
  305.       
  306. /*  save the network  */
  307.  
  308.        ret = krui_saveNet( name, NULL );
  309.        errChk( ret );
  310.        printf( "\nCreate Patterns now\n" );
  311.  
  312. } /* end of create_network */
  313.  
  314. /*****************************************************************************
  315.   FUNCTION : create_patterns
  316.  
  317.   PURPOSE  : if a pattern file is specified patterns will be created and
  318.              converted into the SNNS .pat format
  319.   NOTES    : 
  320.  
  321.   UPDATE   : june 15 1993
  322. ******************************************************************************/
  323.  
  324.  
  325. void create_patterns(char *pattern_file,int no_of_exemplars)
  326. {
  327.     FILE *fp;
  328.     float val;
  329.     int setNo;
  330.  
  331.     if (!strlen(pattern_file) || !no_of_exemplars) 
  332.     return; /* shortcut of no patterns */
  333.  
  334.     /*  create patterns with datas from pattern_file */
  335.        
  336.     if((fp=fopen(pattern_file,"r"))!=NULL){
  337.     ret = krui_allocNewPatternSet(&setNo);
  338.     errChk( ret );
  339.     for (j = 1; j <= no_of_exemplars; j++){
  340.         for (i = 1; i <= IUnits; i++){
  341.         ret = krui_setCurrentUnit(i);
  342.         errChk( ret );
  343.         fscanf(fp,"%s",string);
  344.         val=atof(string);
  345.         krui_setUnitActivation(i,val);
  346.         }
  347.         ret = krui_newPattern();
  348.         errChk( ret );
  349.     }
  350.     } else {
  351.     printf("\n Pattern file %s could not be opened!\n",pattern_file);
  352.     exit(1);
  353.     }
  354.     fclose(fp);
  355.  
  356.     printf("\nFilename of patterns to save: ");
  357.     scanf("%s", name);
  358.     strcat(name,".pat");
  359.     printf("Save Patterns\n");
  360.  
  361.     /*  save the patterns  */
  362.  
  363.     ret = krui_saveNewPatterns( name, setNo );
  364.     errChk( ret );
  365. } /* end of create_patterns */
  366.  
  367. /*****************************************************************************
  368.   FUNCTION : quit 
  369.  
  370.   PURPOSE  : before exiting the Network is deleted
  371.   NOTES    : 
  372.  
  373.   UPDATE   : june 15 1993
  374. ******************************************************************************/
  375.  
  376. void quit(void)
  377. {
  378.        krui_deleteNet();
  379.        exit( 0 );
  380.  
  381. } /* end of quit */
  382.  
  383. /*****************************************************************************
  384.   FUNCTION : main
  385.  
  386.   PURPOSE  : Network Generator for n-Component Kohonen Networks
  387.              using a controlfile 
  388.   NOTES    : 
  389.  
  390.   UPDATE   : june 15 1993
  391. ******************************************************************************/
  392.  
  393. main(int argc,char **argv)
  394. {
  395. FILE *fp;
  396.  
  397.   printf( "\nNetwork Generator for n-Component Kohonen Networks\n" );
  398.   control_file=argv[1];
  399.   if(argc==2)               
  400.     if((fp=fopen(control_file,"r"))!=NULL){
  401.        control(fp);  /* get information from control file */
  402.        if ((IUnits <= 0) || (OUnits < 0) || (HUnits <= 0))
  403.      return( 1 );
  404.        create_network(weight_file);
  405.        create_patterns(pattern_file,no_of_exemplars); /* if any */
  406.        quit();
  407.      }
  408.     else
  409.       printf("\nControl file %s could not be opened!",argv[1]);
  410. else
  411.   printf("\nUsage: convert2snns <control file>");
  412.   printf("\n");
  413.  
  414. } /* main */
  415.  
  416.